package com.hnevc.worldcupserver2.controller;

import com.hnevc.worldcupserver2.bean.User;
import com.hnevc.worldcupserver2.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

@RestController /*Restful API 的控制器*/

public class UserController {
    //注册
    @Autowired
    UserDao userDao;
    @ResponseBody /*方法对应API的返回形式为 json字符串*/
    @PostMapping("/WorldCupApp/user/regist")  /*定义了方法和地址之间的对应关系*/
    public Map<String,Object> register(@RequestBody User user){
        /* @RequestBody 定义请求参数为json字符串类型*/
        HashMap<String,Object> map  =null ;
        User savedUser = userDao.save(user); /*保存数据到数据库中*/
        if ( savedUser!=null ){
            // 注册成功
            map = new HashMap<>();
            map.put("success",true);

            map.put("msg","success!!!!!");

			map.put("data",savedUser);
        }else{
            //注册失败
            map = new HashMap<>();
            map.put("success",false);
            map.put("msg","是啊比");
        }
        return map;
    }
    @RequestMapping("/WorldCupApp/user/login")
    @ResponseBody
    public Map<String,Object> login(@RequestBody User user){
        User userFound = userDao.findOne(Example.of(user)).orElse(null);
        Map<String,Object> map=null;
        if ( userFound!=null ){
            // 注册成功
            map = new HashMap<>();
            map.put("success",true);
            map.put("msg","成功了");
        }else{
            //注册失败
            map = new HashMap<>();
            map.put("success",false);
            map.put("msg","失败");
        }
        return map;
    }
}
